<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 17-4-27
 * Time: 上午9:22
 */

namespace Admin\Controller;

use Think\Controller;
class GoodsController extends Controller{

    public function index()
    {
        $this->assign(array(
            'biaoti'=>'商品首页',
        ));


        $model=D('Goods');
        $datas=$model->search();



        $this->assign(array(
            'datas'=>$datas['data'],
            'page'=>$datas['page'],
        ));
        $this->display();
    }


    public function add()
    {
        $this->assign(array(
            'biaoti'=>'商品添加',
        ));

        $model=D('Goods');
        if(IS_POST){
            //dump($_POST);die;
            if($model->create()){
                if($model->add()){
                    $this->success('添加成功',redirect('index'));
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->error($model->getError());
            }
        }else{
            $cmodel=D('Prodcates');
            $datas=$cmodel->catTree();
            $bmodel=D('Brand');
            $bdatas=$bmodel->catTree();
            $this->bdatas=$bdatas;
            $this->datas=$datas;
            $this->display();
        }
    }

    public function edit($id){
        $this->assign(array(
            'biaoti'=>'商品编辑',
        ));
        $model=D('Goods');
        if(IS_POST){
            if($model->create()){
                if($model->save() !==FALSE){
                    $this->success('修改成功',redirect('index'));
                }else{
                    $this->error('修改失败');
                }
            }else{
                $this->error($model->getError());
            }
        }else{
            $cmodel=D('Prodcates');
            $datas=$cmodel->catTree();
            $this->datas=$datas;
            $row=$model->find($id);
            $this->assign('row',$row);
            $bmodel=D('Brand');
            $bdatas=$bmodel->catTree();
            $this->bdatas=$bdatas;
            $this->display();
        }
    }

    public function del($id){

        $model=M('Goods');
        $model->delete($id);
        $this->success('操作成功');

    }
} 